package com.ricchieri.manage.bean;

import java.util.Date;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.springframework.security.core.context.SecurityContextHolder;

import com.ricchieri.common.CustomAuthenticationToken;
import com.ricchieri.filter.ScheduleCharterFilter;
import com.ricchieri.lazymodel.ScheduleCharterLazyDataModel;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IScheduleCharterService;
import com.ricchieri.service.interf.ITripService;

@ManagedBean(name = "searchNextTravelsMB")
@ViewScoped
public class SearchNextTravelsManageBean extends GenericManageBean {

	private static final long serialVersionUID = -3624916374734996164L;
	
	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;
	
	@ManagedProperty(value = "#{ScheduleCharterService}")
	IScheduleCharterService scheduleCharterService;

	private Map<String, String> trips;
	private ScheduleCharterFilter scheduleCharterFilter;
	private ScheduleCharterLazyDataModel scheduleCharterLazyDataModel;
	
	public ScheduleCharterFilter getScheduleCharterFilter() {
		if(scheduleCharterFilter == null) {
			scheduleCharterFilter = new ScheduleCharterFilter();
			scheduleCharterFilter.setStart(new Date());
			scheduleCharterFilter.setTrip(null);
			
			User currentUser = ((CustomAuthenticationToken)SecurityContextHolder.getContext().getAuthentication()).getUser();
			scheduleCharterFilter.setUserId(currentUser.getId());
		}
		return scheduleCharterFilter;
	}

	public void setScheduleCharterFilter(ScheduleCharterFilter scheduleCharterFilter) {
		this.scheduleCharterFilter = scheduleCharterFilter;
	}

	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}

	public Map<String, String> getTrips() {
		if(trips == null) {
			trips =  getTripService().getComboTrips();
		}
		return trips;
	}

	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}
	
	public void reset() {
		this.scheduleCharterFilter = null;
		this.scheduleCharterLazyDataModel = null;
	}
	
	public void search() {
		this.scheduleCharterLazyDataModel = null;
	}

	public ScheduleCharterLazyDataModel getScheduleCharterLazyDataModel() {
		if(scheduleCharterLazyDataModel == null) {
			scheduleCharterLazyDataModel = new ScheduleCharterLazyDataModel(scheduleCharterService, getScheduleCharterFilter());
		}
		return scheduleCharterLazyDataModel;
	}

	public void setScheduleCharterLazyDataModel(
			ScheduleCharterLazyDataModel scheduleCharterLazyDataModel) {
		this.scheduleCharterLazyDataModel = scheduleCharterLazyDataModel;
	}

	public IScheduleCharterService getScheduleCharterService() {
		return scheduleCharterService;
	}

	public void setScheduleCharterService(
			IScheduleCharterService scheduleCharterService) {
		this.scheduleCharterService = scheduleCharterService;
	}
}

